package com.moogene.saad.gameplay.stages;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;

import com.moogene.saad.gameplay.UserState;

public class BattleFieldsManger {

	private final HashMap<String, SoftReference<BattleField>> softBattleField;

	private static BattleFieldsManger sBattleFieldsManger;

	private BattleFieldsManger() {
		softBattleField = new HashMap<String, SoftReference<BattleField>>();
	}

	public static BattleFieldsManger getInstance() {
		if (sBattleFieldsManger == null) {
			sBattleFieldsManger = new BattleFieldsManger();
		}
		return sBattleFieldsManger;
	}

	public BattleField getBattleField(String name) {
		BattleField bf = null;
		if (softBattleField.get(name) != null) {
			bf = softBattleField.get(name).get();
		}
		if (bf == null) {
			bf = BattleField.load(name);
		}
		softBattleField.put(name, new SoftReference<BattleField>(bf));
		return bf;
	}
	
	public BattleField getFirstBattleField(){
		ArrayList<String> battleFieldNames = UserState.getInstance()
				.getBattleFieldNames();
		String firstFieldNames = battleFieldNames.get(0);
		return getBattleField(firstFieldNames);
	}
	
	public BattleField getLastBattleField(){
		ArrayList<String> battleFieldNames = UserState.getInstance()
				.getBattleFieldNames();
		String lastFieldNames = battleFieldNames
				.get(battleFieldNames.size() - 1);
		return getBattleField(lastFieldNames);
	}

}
